package com.yjhb.android.sbz.activity;

import android.Manifest;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.yjhb.android.sbz.R;
import com.jojo.android.zxlib.base.mvp.BasePresenter;
import com.jojo.android.zxlib.tool.ZxFileTool;
import com.jojo.android.zxlib.tool.ZxImageTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 创建者 zx
 * 创建时间 2021/2/2 16:41
 */
public class BusinessActivity extends BaseActivity {
    private final String TAG = "BusinessActivity";
    @BindView(R.id.business_iv)
    ImageView business_iv;
    private final String url = "https://shangbangzhuan.oss-cn-beijing.aliyuncs.com/commonImages/business_icon.jpg";

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_business;
    }

    @Override
    protected void initUI() {
        Glide.with(this)
                .load(url)
                .addListener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        ZxToast.showBottomToastLight("加载失败！请重试！");
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(business_iv);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void regEvent() {

    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }

    @OnClick(R.id.save_btn)
    void save_btn() {
        Disposable disposable =  new RxPermissions(getActivity())
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        Glide.with(getActivity())
                                .asBitmap()
                                .load(url)
                                .into(new CustomTarget<Bitmap>() {
                                    @Override
                                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                        File dir = new File(ZxFileTool.getAppSDFilePath() + "/images");
                                        if (!dir.exists()) {
                                            dir.mkdirs();
                                        }
                                        File file = new File(dir, "赏帮微信.png");
                                        if (!file.exists()) {
                                            try {
                                                file.createNewFile();
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                        boolean b = ZxImageTool.save(resource, file, Bitmap.CompressFormat.PNG);
                                        if (b) {
                                            ZxToast.showSuccessBottomToast("已保存至：" + file.getAbsolutePath());
                                        } else {
                                            ZxToast.showFAILBottomToast("保存失败...");
                                        }
                                    }

                                    @Override
                                    public void onLoadCleared(@Nullable Drawable placeholder) {

                                    }
                                });
                    }
                });
    }


}
